/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.itriage.hospital.services.impl;

import com.itriage.hospital.client.web.web_model.StaffModel;
import com.itriage.hospital.model.Staff;
import com.itriage.hospital.services.StaffService;
import com.itriage.hospital.services.crud.StaffCrudServices;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Yandiswa
 */
@Service("staffService")
public class StaffServiceImpl implements StaffService{
    @Autowired
    private StaffCrudServices staffCrudServices;
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)

    
    public List<Staff> createStaff(StaffModel model) {
        
    Staff staff = new Staff(); // DONT DO THIS USE FACTORY
    staff.setStaffNum(model.getStaffNum());
    staff.setEducation(model.getEducation());
    staffCrudServices.persist(staff);
    return staffCrudServices.findAll();
    
    }

    @Override
    public List<Staff> deleteStaff(Long id) {
        
     staffCrudServices.removeById(id);
     return staffCrudServices.findAll();
    }

    @Override
    public StaffModel getStaffToEdit(Long id) {
       
     StaffModel model = new StaffModel();
        Staff staff = staffCrudServices.findById(id);
        model.setStaffNum(staff.getStaffNum());
        model.setEducation(staff.getEducation());
        model.setId(id);
        return model;
    }

    @Override
    public List<Staff> updateStaff(StaffModel model) {
       
    Staff staff = new Staff(); // DONT DO THIS USE FACTORY
        staff.setStaffNum(staff.getStaffNum());
        staff.setEducation(staff.getEducation());
        staff.setId(model.getId());
        staffCrudServices.merge(staff);
        return staffCrudServices.findAll();
    }

    @Override
    public List<Staff> getStaff() {
    return staffCrudServices.findAll(); 
    
    }
    
}
